package com.angel.controller;

import com.angel.bean.IllCase;
import com.angel.bean.Prescription;
import com.angel.bean.dto.IllCaseDto;
import com.angel.dto.getOrderIdByUUId;
import com.angel.service.IllCaseService;
import com.angel.service.OtherlogService;
import com.angel.service.PrescriptionService;
import com.angel.shiro.SystemAdminAuthorizingRealm;
import com.angel.utils.AdminUserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Timestamp;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/illCaseController")
public class IllCaseController {

    @Resource
    private IllCaseService illCaseService;
    @Autowired
    private OtherlogService otherlogService;
    @Autowired
    private PrescriptionService prescriptionService;
    /**
     * 添加
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/addIllCase")
    @ResponseBody
    public Object addIllCase(IllCase illCase) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        // 档案编号未设置
        illCase.setState(1);
        int result = illCaseService.insert(illCase);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /***
     * 更新
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     * @param illCase 前端传入id值
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/updateIllCase")
    @ResponseBody
    public Object updateIllCase(IllCase illCase) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        int result = illCaseService.update(illCase);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /***
     * 删除
     *
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     * @param illCase 前端传入id值
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/deleteIllCase")
    @ResponseBody
    public Object deleteIllCase(IllCase illCase) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        illCase.setState(0);
        int result = illCaseService.delete(illCase);
        if (result != 0) {
            map.put("result", "success");
        }
        return map;
    }

    /**
     * 查询单个
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/findOneIllCase")
    @ResponseBody
    public Object findOneIllCase(IllCase illCase) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        illCase.setState(1);
        IllCase one = illCaseService.findOne(illCase);
        if (one != null) {
            map.put("one", one);
        }
        return map;
    }

    /**
     * 查询所有
     *
     * @return
     * @RequiresPermissions("user") 权限控制器必须登录才能访问
     */
    @RequiresPermissions("user")
    @RequestMapping("/findIllCase")
    public String findIllCase(Model model, IllCase illCase) {

        illCase.setState(1);

        List<IllCase> illCaseList = illCaseService.findIllCase(illCase);

        if (illCaseList != null) {
            model.addAttribute("illCaseList", illCaseList);
        }
        return "illCaseEntry";
    }

    @RequestMapping("/InsertSecondCase")
    @ResponseBody
    public Object InsertSecondCase(IllCase illCase) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        illCase.setIllCaseNo(getOrderIdByUUId.getOrderIdByUUId());
        illCase.setState(1);
        illCase.setVisitDate(new Timestamp(System.currentTimeMillis()));
        illCase.setIllCaseState(0);
        if (illCase.getBabyId() != null || illCase.getUserId() != null) {
            int flag = illCaseService.insertSelective(illCase);
            if (flag == 0) {
                map.put("data", "失败");
            } else {
                map.put("data", "成功");
            }
        }
        return map;
    }


    @RequestMapping("/updateSecondCase")
    @ResponseBody
    public Object UpdateSecondCase(IllCase illCase) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        illCase.setIllCaseNo(getOrderIdByUUId.getOrderIdByUUId());
        illCase.setState(1);
        illCase.setVisitDate(new Timestamp(System.currentTimeMillis()));
        illCase.setIllCaseState(0);
        if (illCase.getIllCaseId() != null || illCase.getUserId() != null) {
            int flag = illCaseService.update(illCase);
            if (flag == 0) {
                map.put("data", "失败");
            } else {
                map.put("data", "成功");
            }
        }
        return map;
    }


    /**
     * 将症状详情根据ID插入相应的表中
     */
    @RequestMapping("/InsertSysmpById")
    @ResponseBody
    public Object InsertSysmpById(HttpServletRequest request) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        IllCase illCase = new IllCase();
        illCase.setSysmptom(request.getParameter("sysmptype"));
        illCase.setIllCaseId(Integer.parseInt(request.getParameter("id")));
        illCase.setIllCaseState(Integer.parseInt(request.getParameter("illCaseState")));

        int count = illCaseService.update(illCase);
        if (count > 0) {
            map.put("data", "更新成功");
        } else {
            map.put("data", "服务器内部错误");
        }
        return map;
    }


    @RequestMapping("/getillcase")
    public String getIllcaseAll(@RequestParam(value = "status", defaultValue = "5") String status, @RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        int st = 5;
        try {
            pageNo = Integer.valueOf(pn);
            st = Integer.valueOf(status);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 10);
        SystemAdminAuthorizingRealm.Principal principal = AdminUserUtils.getPrincipal();
        int branchesId = principal.getEmployee().getBranchesId();
        IllCaseDto tmp = new IllCaseDto();
        tmp.setIllCaseState(st);
        tmp.setBranid(branchesId);
        List<IllCaseDto> illCaseDtos = illCaseService.getIllCaseAndBabyInfoAndUserInfo(tmp);
        PageInfo pageInfo = new PageInfo(illCaseDtos);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("status",st);
        if (st == 5) {
            return "preaboutdetils";
        } else {
            return "finalprocess";
        }
    }

    /**
     * 通过病例id获取处方详情
     * @param illid
     * @return
     */
    @RequestMapping("/getpre")
    @ResponseBody
    public Map getPre(@RequestParam(value = "illid") String illid){
        Map map = new HashMap();
        map.put("success",false);
        Integer id = null;
        try {
            id = Integer.valueOf(illid);
        }catch (Exception e){}
        Prescription prescription = new Prescription();
        prescription.setIllCaseId(id);
        prescription = prescriptionService.findon(prescription);
        if (prescription!=null){
            map.put("data",prescription);
            map.put("success",true);
        }else {
            map.put("msg","参数异常");
        }
        return map;
    }
}
